// ErrorBoundary.tsx
import React from 'react';
import { View, Text } from 'react-native';

export class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  // React 触发此方法更新状态
  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  // 记录错误信息
  componentDidCatch(error, info) {
    console.log('ErrorBoundary 捕获到错误:', error, info);
    // 可上报日志到远程服务
  }

  render() {
    if (this.state.hasError) {
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Text style={{ color: 'red' }}>出错了，请稍后重试 😢</Text>
        </View>
      );
    }
    return this.props.children;
  }
}
